﻿using System;
using System.Diagnostics;
using System.IO;

namespace DNFImagePacks2Manager.Helpers
{
    public static class ConsoleHelper
    {
        public static bool OpenFolderInExplorer(string? folderPath)
        {
            if (string.IsNullOrEmpty(folderPath))
            {
                return false;
            }

            if (!Directory.Exists(folderPath))
            {
                if (File.Exists(folderPath))
                {
                    folderPath = Path.GetDirectoryName(folderPath);
                }
                else
                {
                    return false;
                }
            }

            try
            {
                Process.Start(new ProcessStartInfo
                {
                    FileName = "explorer.exe",
                    Arguments = $"\"{folderPath}\"",
                    UseShellExecute = true,
                    CreateNoWindow = true
                });

                return true;
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"打开资源管理器异常：{ex.Message}");
                return false;
            }
        }

        public static bool OpenFolderAndSelectFile(string filePath)
        {
            if (!File.Exists(filePath))
            {
                Debug.WriteLine($"打开资源管理器失败：文件不存在 - {filePath}");
                return false;
            }

            try
            {
                Process.Start(new ProcessStartInfo
                {
                    FileName = "explorer.exe",
                    Arguments = $"/select,\"{filePath}\"",
                    UseShellExecute = true,
                    CreateNoWindow = true
                });

                return true;
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"打开资源管理器并选中文件异常：{ex.Message}");
                return false;
            }
        }

        public static bool OpenFileInNotepad(string filePath)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                return false;
            }

            if (!File.Exists(filePath))
            {
                return false;
            }

            try
            {
                Process.Start(new ProcessStartInfo
                {
                    FileName = "notepad.exe",
                    Arguments = $"\"{filePath}\"",
                    UseShellExecute = true,
                    CreateNoWindow = true
                });

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}